package com.huawei.statistics.schedule.job;


import com.huawei.statistics.service.member.MemberService;
import com.huawei.statistics.service.schedule.quartz.ScheduleService;
import com.huawei.statistics.service.video.VideoService;
import com.huawei.statistics.utils.DateTimeUtilT;
import com.huawei.statistics.vo.common.HttpResult;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Calendar;
import java.util.Date;

/**
 * 自定义定时器
 *
 * create BY yinwx on  2018/05/02
 *
 */

public class MyQuartzJobBean extends QuartzJobBean{


    @Autowired
    MemberService memberService;
    @Autowired
    ScheduleService scheduleService;
    @Autowired
    VideoService videoService;



    public static final String GET_AQIYIVIDEO = "getAQIYI";//获取爱奇艺视频
    public static final String GET_TENGXUNVIDEO = "getTENGXUN";//获取腾讯视频




    public Logger logger = Logger.getLogger(QuartzJobBean.class);


    @Override
    protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
        org.quartz.Trigger trigger = jobexecutioncontext.getTrigger();
        String triggerName = trigger.getKey().getName();
        String groupName = trigger.getKey().getGroup();
        logger.info("正在执行任务【" + triggerName + "】，group【" + groupName + "】"+" time:"+ DateTimeUtilT.dateTime(trigger.getStartTime())+",当前线程："+Thread.currentThread().getName());
        switch (groupName) {

            case GET_AQIYIVIDEO:
                try{
                    System.out.println("爱奇艺爬取定时器"+triggerName);
                    //电影
                    for(int i=30;i>0;i--){
                        HttpResult result = videoService.getAiqiyiVideo(i);
                    }
                    //电视剧
                    for(int i=30;i>0;i--){
                        videoService.getAiqiyiTYplay(i);
                    }
                    //动漫
                    for(int i=30;i>0;i--){
                        videoService.getAiqiyiComic(i);
                    }

                    videoService.getTVPlayDefailt(2);
                    videoService.getTVPlayDefailt(4);
                    videoService.uploadImageToOss();

                }catch (Exception e){
                    System.out.println("视频更新出错"+e.getMessage());

                }finally {
                    Calendar cal1 = Calendar.getInstance();
                    cal1.add(Calendar.DAY_OF_MONTH,+1);
                    cal1.set(Calendar.HOUR_OF_DAY,4);
                    cal1.set(Calendar.MINUTE,0);
                    cal1.set(Calendar.SECOND,0);
                    System.out.println(DateTimeUtilT.dateTime(cal1.getTime())+"开启了一个定时器");
                    scheduleService.scheduleEvenIfDateAfterNow("getAIQIYIVIDEO", cal1.getTime(), MyQuartzJobBean.GET_AQIYIVIDEO);
                    break;
                }

            case GET_TENGXUNVIDEO:
                System.out.println("会员组设置完毕");
                break;


        }
    }

    private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
        try {
            return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey");
        } catch (SchedulerException e) {
            System.out.println("jobexecutioncontext.getScheduler().getContext() error!");
            throw new RuntimeException(e);
        }
    }
}
